# -*- coding: utf-8 -*-
import requests
import json
from common.operateExcel import ExcelUtil
# 引入上下文接口传参时需要的 参数：dic_sav_par
from common.commonUtil import get_varparam, set_varparam, dic_sav_par


# 接口请求
class sendRequest():
    def __init__(self):
        self.url = None
        self.method = None
        self.headers = None
        self.bodydata = None

    # def send_requests(self, exceldata, other_header=None):
    #     """ 封装requests请求 """
    #     self.method = exceldata["request_method"]
    #     url = exceldata["host"] + exceldata["api"]
    #     headers = {'Content-Type': 'application/json'}
    #     # url后面的params参数
    #     if exceldata["url_params"] is None or exceldata["url_params"] == "":
    #         self.url = url
    #     else:
    #         self.url = url + "?" + exceldata["url_params"]
    #     print("*******正在执行用例**********")
    #     print("请求方式：%s, 请求url:%s" % (self.method, self.url))
    #     # 请求头部headers, 默认一个header
    #     if exceldata["request_headers"] is None or exceldata["request_headers"] == "" or other_header is None:
    #         self.headers = headers
    #     else:
    #         head_tmp = eval(exceldata["request_headers"])
    #         headers.update(other_header)
    #         headers.update(head_tmp)
    #         self.headers = headers
    #     print("请求headers：%s" % self.headers)
    #     # 请求body内容
    #     if exceldata["formData"] is None or exceldata["formData"] == "":
    #         pass
    #     else:
    #         body_tmp = eval(exceldata["formData"])
    #         self.bodydata = json.dumps(body_tmp)
    #     print("请求body内容为：%s" % self.bodydata)
    #     if self.method == "post":
    #         res = self.send_post(self.url, self.headers, self.bodydata)
    #     elif self.method == "get":
    #         res = self.send_get(self.url, self.headers, self.bodydata)
    #     else:
    #         print("暂未支持")
    #         res = "method error"
    #     # 拿到返回信息后，保存 上下文接口运行需要的参数
    #     if exceldata["save_param"] is None or exceldata["save_param"] == "":
    #         pass
    #     else:
    #         dic_tmp = set_varparam(data, "save_param", res)
    #         print("保存的参数信息 {0}".format(dic_tmp))
    #     return res

    def send_requests(self, exceldata):
        """ 封装requests请求 """
        self.method = exceldata["request_method"]
        # 拼接url
        api = set_varparam(exceldata["api"], dic_sav_par)
        url = exceldata["host"] + api
        # url后面的params参数
        if exceldata["url_params"] is None or exceldata["url_params"] == "":
            self.url = url
        else:
            url_pa = set_varparam(exceldata["url_params"], dic_sav_par)
            self.url = url + "?" + url_pa
        print("*******正在执行用例**********")
        print("请求方式：%s, 请求url:%s" % (self.method, self.url))
        use_headers = {'Content-Type': 'application/json'}
        # 请求头部headers, 默认一个header
        if exceldata["request_headers"] is None or exceldata["request_headers"] == "":
            self.headers = use_headers
        else:
            req_head = set_varparam(exceldata["request_headers"], dic_sav_par)
            head_tmp = eval(req_head)
            use_headers.update(head_tmp)
            self.headers = use_headers
        print("请求headers：%s" % self.headers)
        # 请求body内容
        if exceldata["formData"] is None or exceldata["formData"] == "":
            pass
        else:
            fo_da = set_varparam(exceldata["formData"], dic_sav_par)
            body_tmp = eval(fo_da)
            self.bodydata = json.dumps(body_tmp)
        print("请求body内容为：%s" % self.bodydata)
        if self.method == "post":
            res = self.send_post(self.url, self.headers, self.bodydata)
        elif self.method == "get":
            res = self.send_get(self.url, self.headers, self.bodydata)
        else:
            print("暂未支持")
            res = "method error"
        # 拿到返回信息后，保存 上下文接口运行需要的参数
        if exceldata["save_param"] is None or exceldata["save_param"] == "":
            pass
        else:
            dic_tmp = get_varparam(exceldata, "save_param", res)
            # print("保存的参数信息 {0}".format(dic_tmp))
        return res

    def send_get(self, url, headers, bodydata):
        r = requests.get(url, headers=headers, data=bodydata)
        print("页面返回信息：%s" % r.content.decode("utf-8"))
        res = json.loads(r.content.decode("utf-8"))
        return res

    def send_post(self,url, headers, bodydata):
        r = requests.post(url, headers=headers, data=bodydata)
        print("页面返回信息：%s" % r.content.decode("utf-8"))
        res = json.loads(r.content.decode("utf-8"))
        return res


if __name__ == "__main__":
    data = ExcelUtil("../case/test-yeyun.xlsx").dict_data()
    print(type(data[3]["url_params"]))
    ress = sendRequest().send_requests(data[3])
    print(ress)
